package cn.edu.nju.software.cdiy.action.user.login;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.User;
import cn.edu.nju.software.cdiy.service.impl.UserServiceImpl;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserInfoAction extends ActionSupport implements SessionAware, ServletResponseAware {

	private Map<String, Object> session;
	private HttpServletResponse response;
	private UserServiceImpl userService;
	private User user;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("UTF-8");
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void modify() throws Exception {
		User sessionUser = (User) session.get(BusinessConstant.S_USER);
		sessionUser.setEmail(user.getEmail());
		sessionUser.setRealName(user.getRealName());
		sessionUser.setGender(userService.getGender(user.getGender().getKey()));
		sessionUser.setBirthday(user.getBirthday());
		sessionUser.setPhone(user.getPhone());
		sessionUser.setAddress(user.getAddress());
		sessionUser.setDeliveryAddress(user.getDeliveryAddress());
		sessionUser.setPostcode(user.getPostcode());

		User newUser = userService.updateUser(sessionUser);
		if (newUser != null) {
			session.put(BusinessConstant.S_USER, newUser);
			response.getWriter().write("修改成功！");
		} else {
			response.getWriter().write("修改失败！");
		}
	}

}
